Skip to content

Conversation

@reosfire
Copy link
Contributor

@reosfire reosfire commented May 6, 2025

We can just copy data from buffer to buffer instead of making clone when buffers size match.

@reosfire reosfire force-pushed the reduce-buffer-allocations branch from a506f2f to 3d8dee4 Compare May 6, 2025 22:36
@soywiz soywiz requested a review from Copilot May 7, 2025 07:56
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

//println(data.sizeInBytes)
// Only check small buffers
if (data.sizeInBytes < 1024) {
if (this.mem != null && this.mem!!.sizeInBytes == data.sizeInBytes && arrayequal(this.mem!!, 0, data, 0, data.sizeInBytes)) return this
Copy link
Contributor

@soywiz soywiz May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part deleted? I guess the point is not being marked as dirty, so it doesn't need to be uploaded to the GPU again.

@soywiz soywiz requested a review from Copilot May 7, 2025 08:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR aims at reducing buffer allocations in the AGBuffer.upload methods by copying data directly when the buffer sizes match, instead of always cloning the buffer.

  • Refactored overloaded upload functions for various data types to pass a needClone flag.
  • Introduced a private upload method that conditionally copies or clones the buffer based on the existing memory size.

upload(data, needClone = true)

private fun upload(data: Buffer, needClone: Boolean): AGBuffer {
if (mem?.sizeInBytes == data.sizeInBytes) {
Copy link

Copilot AI May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a check to compare the content of mem and data for small buffers, as the previous implementation did, to avoid unnecessary copying when the existing buffer already matches the new data.

Copilot uses AI. Check for mistakes.
@soywiz soywiz merged commit 2b224f3 into korlibs:main May 7, 2025
6 checks passed
@soywiz
Copy link
Contributor

soywiz commented May 7, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants